package com.yidian.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.yidian.dao.SeckillGoodsMapper;
import com.yidian.pojo.seckill.SeckillGoods;
import com.yidian.pojo.seckill.SeckillOrder;
import com.yidian.pojo.seckill.SeckillStatus;
import com.yidian.server.pay.WeixinPayService;
import com.yidian.server.seckill.SeckillGoodsService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

/**
 * @author whb
 * @create 2021-11-01 16:10
 * @desciption rabbitmq延时消息监听类
 */
public class OrderMessageListener implements MessageListener {
    @Autowired
    private RedisTemplate redisTemplate;
    @Reference
    private WeixinPayService weixinPayService;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    /**
     * 消息监听
     *
     * @param message
     */
    @Override
    public void onMessage(Message message) {
        String content = new String(message.getBody());
        System.out.println("监听到未支付消息是：" + content);
        rollbackOrder(JSON.parseObject(content, SeckillStatus.class));//回滚订单
    }

    /**
     * 回滚订单
     *
     * @param seckillStatus
     */
    private void rollbackOrder(SeckillStatus seckillStatus) {
        /**
         * 监听消息为空，不做处理
         */
        if (seckillStatus == null) {
            return;
        }
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.boundHashOps("SeckillOrder").get(seckillStatus.getUsername());//判断redis缓存是否存在对应的订单数据
        if (seckillOrder != null) {
            Map<String, String> map = weixinPayService.closePay(seckillStatus.getOrderId().toString());//关闭微信订单
            if ("SUCCESS".equals(map.get("return_code")) && "SUCCESS".equals(map.get("result_code"))) {
                redisTemplate.boundHashOps("SeckillOrder").delete(seckillOrder.getUserId());//清除redis缓存中的订单数据
                SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_" + seckillStatus.getTime()).get(seckillStatus.getGoodsId());//读取redis缓存中秒杀商品的数
                if (seckillGoods == null) {//如果redis缓存中不存在秒杀商品数据
                    seckillGoods = seckillGoodsMapper.selectByPrimaryKey(seckillStatus.getGoodsId());//从mysql数据库中查询商品数据
                }

                //回滚库存
                Long seckillGoodsCount = redisTemplate.boundHashOps("SeckillGoodsCount").increment(seckillStatus.getGoodsId(), 1);
                seckillGoods.setStockCount(seckillGoodsCount.intValue());

                //商品数据同步到redis缓存
                redisTemplate.boundHashOps("SeckillGoods_" + seckillStatus.getTime()).put(seckillStatus.getGoodsId(), seckillGoods);
                redisTemplate.boundListOps("SeckillGoodsCountList_" + seckillStatus.getGoodsId()).leftPush(seckillStatus.getGoodsId());

                //seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);//商品数据同步到数据库

                redisTemplate.boundHashOps("UserQueueCount").delete(seckillStatus.getUsername());//清除redis缓存中重复排队标识
                redisTemplate.boundHashOps("UserQueueStatus").delete(seckillStatus.getUsername());//清除redis缓存中秒杀状态数据
            }
        }
    }
}
